<?
require("config.php");

$label = "Order List";

$table="cus_order";

function decode($str){
/*	$output=preg_replace_callback("|&#(\d+);|",
             "convertstr",
             $str);
	return $output;	
*/
//return html_entity_decode ($str);
return $str;


}

function convertstr($matches){
	echo(strtoupper('%'.substr(str_pad(dechex($matches[1]),4,'0',STR_PAD_LEFT),0,2).'%'.substr(str_pad(dechex($matches[1]),4,'0',STR_PAD_LEFT),2,2)));
// return iconv("BIG5","ISO-8859-1",chr(hexdec(substr(str_pad(dechex($matches[1]),4,'0',STR_PAD_LEFT),2,2))).chr(hexdec(substr(str_pad(dechex($matches[1]),4,'0',STR_PAD_LEFT),0,2))));
}

if ($action=="search"){

	$ftime=strtotime($fy.'-'.$fm.'-'.$fd.' 00:00:00');
	$ttime=strtotime($ty.'-'.$tm.'-'.$td.' 23:59:59');

	if ($no) $nostr=' and no = '.$no;
	else $nostr='';

	if ($firstname) $namestr="%$firstname%";
	else $namestr='';
	if ($lastname) 	$namestr.="$lastname%";
	if ($namestr) 	$namestr=" and member_name like '$namestr'";
	
	if ($member_email) $emailstr=" and member_email like '%$member_email%'";
	else $emailstr='';
	
	if ($hkid) $hkidstr=" and hkid like'%$hkid%'";
	else $hkidstr='';
	

	$Query="SELECT 
	id,
	no,
	trans_no,
	member_name,
	member_email,
	purchase_date,
	order_status,
	shipping_status,
	delivery_date,
	shipping_carrier,
	shipping_region
	 
	FROM $table WHERE 
	purchase_date	between $ftime and $ttime
	$nostr
	$namestr
	$emailstr
	$hkidstr
	ORDER BY id DESC";

	$r=myQuery($Query);

}else{

	$Query="SELECT 
	id,
	no,
	trans_no,
	member_name,
	member_email,
	purchase_date,
	order_status,
	shipping_status,
	delivery_date,
	shipping_carrier,
	shipping_region
	FROM $table ORDER BY id DESC";

	$r=myQuery($Query);

}

//header("Content-type:application/vnd.ms-excel");  
//header("Content-Disposition:filename=orderlist_" . date("Ymd") . ".xls");  



echo "Order No\t";  
echo "Transaction No\t";
echo "Customer Name\t";
echo "Customer Email\t"; 
echo "Order Date\t"; 
echo "Order Status\t";
echo "Shipping Status\t";
echo "Delivery Date\t";
echo "Order Amount\t\n";
for ($i=0;$i<count($r);$i++){

$shipping_carrier=$r[$i][9];
$shipping_region=$r[$i][10];
//echo $shipping_carrier."\t";
//echo $shipping_region."\t";

$Query = "SELECT 
t.no,
t.pro_code,
p.name,
t.pro_color,
t.pro_size,
t.unit_price,
t.qty,
t.id ,p.weight,
p.brand
FROM cus_order_sub as t,pro_basic as p WHERE t.no='".$r[$i][1]."' and t.pro_code=p.code";
$sub=myQuery($Query);
$total_qty = 0;
$total_price = 0;
$weight=0;
$Carrierfee=0;
	for ($j=0;$j<count($sub);$j++){
	
	$subtotal = $sub[$j][5]*$sub[$j][6];
	$total_qty += $sub[$j][6];
	$total_price += $subtotal;
	$weight += $sub[$j][6]*$sub[$j][8];
	}
	if ($shipping_carrier=='Self Take') $Carrierfee=0;
	else {
		$sql="select charge,weightto from med_shipping where region='$shipping_region' and carrier='$shipping_carrier' order by weightto desc limit 0,1";
		$sub=myQuery($sql);
		$maxcharge	= $sub[0][0];
		$maxweight	= $sub[0][1];
	
		if ($weight<=$maxweight) { 
			$sql="select charge from med_shipping where region='$shipping_region' and carrier='$shipping_carrier' and $weight >= weightfrom and $weight <= weightto;";
			$sub=myQuery($sql);
			$Carrierfee= $sub[0][0];
		}
		else {
			if($maxweight==0) {
				$Carrierfee= 0;
				$subweight	= 0;
			}
			else {
				$Carrierfee= floor($weight/$maxweight)*$maxcharge;
				$subweight	= $weight - $maxweight * floor($weight/$maxweight);
			}
			$sql="select charge from med_shipping where region='$shipping_region' and carrier='$shipping_carrier' and $subweight >= weightfrom and $subweight <= weightto;";
			$sub=myQuery($sql);
			$Carrierfee	+= $sub[0][0];
		}
	}

	if ($carrier=='Express') {
			$sql="select value from misc_data where name='express'";
			$sub=myQuery($sql);
			$rate=$sub[0][0];
			$Carrierfee=round($Carrierfee*(100+$rate)/100,2);
	}



if ($r[$i][8]>strtotime('1980-01-01')) $date=date('Y-m-d',$r[$i][8]);
else $date='';

echo str_pad($r[$i][1],5,'0',STR_PAD_LEFT). "\t"; 
echo $r[$i][2] . "\t"; 
echo decode($r[$i][3])."\t";
//echo iconv("ISO-8859-1","UTF8",$r[$i][3]) . "\t"; 
echo $r[$i][4] . "\t"; 
echo date('Y-m-d H:i:s',$r[$i][5]) . "\t"; 
echo $r[$i][6] . "\t"; 
echo $r[$i][7] . "\t"; 
echo $date . "\t"; 
echo number_format($total_price+$Carrierfee,2) . "\t\n"; 
}

echo "\n"; 



?>